<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="订单编号" prop="orderSn">
        <el-input
          v-model="queryParams.orderSn"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="采购时间" prop="addTime">
        <el-date-picker
          v-model="range"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="shoppingOrderList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="orderId" />
      <el-table-column label="订单编号" align="center" prop="orderSn" />
      <el-table-column label="订单总金额" align="center" prop="totalAmount" />
      <el-table-column label="采购员" align="center" prop="accountId" />
      <el-table-column label="采购时间" align="center" prop="addTime" />
      <el-table-column label="状态" align="center" prop="isBuy">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.isBuy == 1">已支付</el-tag>
          <el-tag type="danger" v-if="scope.row.isBuy == 0">未支付</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="handleExport(scope.row)"
            v-hasPermi="['cancel:shoppingOrder:import']"
            >导出</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row)"
            v-hasPermi="['cancel:shoppingOrder:detail']"
            >查看详情</el-button
          >
          <el-button
            v-if="scope.row.isBuy == 0"
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handlePay(scope.row)"
            v-hasPermi="['cancel:shoppingOrder:pay']"
            >支付</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改采购单对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1000px"
      append-to-body
    >
      <el-table v-loading="loading" :data="goodsList">
        <el-table-column label="商品名称" align="center" prop="productName" />
        <el-table-column label="商品个数" align="center" prop="productNum" />
        <el-table-column label="商品单价" align="center" prop="productPrice" />
        <el-table-column label="总金额" align="center" prop="totalPrice" />
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listShoppingOrder,
  goodsList,
  changeBuyStatus,
} from "@/api/order/shoppingOrder";

export default {
  name: "ShoppingOrder",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购单表格数据
      shoppingOrderList: [],
      //采购单下的商品列表
      goodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderSn: null,
      },
      range: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询采购单列表 */
    getList() {
      this.loading = true;
      const [startTime, endTime] = this.range;
      this.queryParams.startTime = startTime;
      this.queryParams.endTime = endTime;
      listShoppingOrder(this.queryParams).then((response) => {
        this.shoppingOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderId: null,
        mallProductId: null,
        orderSn: null,
        totalAmount: null,
        accountId: null,
        addTime: null,
        productId: null,
        isBuy: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.orderId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },

    handleDetail(row) {
      const { orderId } = row;
      this.open = true;
      goodsList({ orderId }).then((res) => {
        this.loading = false;
        this.goodsList = res.data;
      });
    },
    //支付
    handlePay(row) {
      const { orderId } = row;
      this.$modal
        .confirm("是否确认已支付？")
        .then(function () {
          return changeBuyStatus({ orderId, isBuy: 1 });
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("修改成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport(row) {
      const { orderId } = row;
      this.download(
        "cancel/shoppingOrder/exportCartList",
        {
          ...this.queryParams,
          orderId,
        },
        `shoppingOrder_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
